<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/7/28
 * Time: 20:25
 */

class QueryServerInfo extends ApiRedisBase
{
    public $roomRedis;

    public function logic()
    {
        $this->checkUser();
        //如果已存在房间，只许跳到该房间
        $userQuery = new UserQuery();
        $roomInfo  = $userQuery->queryRoomInfo($this->uid, $this->platform);
        $this->roomRedis = $this->redis('room_status');

        //不存在已有房间，才可以加入其他房间
        if (empty($roomInfo)) {
            $roomID = $this->params['roomID'];
            if (empty($roomID)) {
                $this->returnError('no_room_id');
            }

            if (!$this->roomRedis->exists('R:' . $roomID)) {
                $this->returnError('no_room_id');
            }

            $roomInfo = $this->roomRedis->HMget(
                'R:' . $roomID, array('roomID', 'serverID')
            );
        }

        $this->i('QueryServerInfo_serverID', ['serverID' => $roomInfo['serverID']]);
        if (!$this->redis->exists('S:' . $roomInfo['serverID'])) {
            $this->returnError('no_server', -1, ['serverID' => $roomInfo['serverID']]);
        }

        $serverInfo = $this->redis->HMget(
            'S:' . $roomInfo['serverID'], array('ip', 'serverPort')
        );

        if (empty($serverInfo['ip']) || empty($serverInfo['serverPort'])) {
            $this->returnError('no_server_info');
        }

        $data = array(
            'roomID' => $roomInfo['roomID'],
            'serverIP' => (string)$serverInfo['ip'],
            'serverPort' => (integer)$serverInfo['serverPort'],
        );

        $this->returnData('suc_query_server', $data);
    }
}